import java.util.HashMap;

public class Test06 {

    // 移动0
    public void moveZeroes(int[] nums) {
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            // i 一直走 遇到不为0 的 赋值给 index下标然后index++ (也就是把不为0的值往前挪)
            if (nums[i] != 0){
                nums[index++] = nums[i];
            }
        }
        // 直到 i 遍历完之后,说明不为0的数已经全部挪到前面了, 剩下index 到 nums.length-1 之间的数组都是0
        while (index < nums.length){
            nums[index++] = 0;
        }
    }

    public void moveZeroes2(int[] nums){
        int i = 0;
        for (int j = 0; j < nums.length; j++) {
            if (nums[j] != 0){
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j] = tmp;
                i++;
            }
        }
    }


    // 两数之和
    // 哈希表
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.get(target - nums[i]) != null){
                return new int[] {i,map.get(target-nums[i])};
            }
            map.put(nums[i],i);
        }
        return new int[] {0,0};
    }


    // 反转字符串
    public void reverseString(char[] s) {
        int left = 0;
        int right = s.length - 1;
        while (left < right){
            char tmp = s[left];
            s[left++] = s[right];
            s[right--] = tmp;
        }
    }

}
